package com.example.demo.model.admin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Results.Result;
import com.example.demo.model.admin.admin.entity.EditPassReq;
import com.example.demo.model.admin.admin.entity.ShopAdmin;
import com.example.demo.model.admin.admin.entity.reg.LoginReg;
import com.example.demo.model.admin.admin.mapper.ShopAdminDao;
import com.example.demo.model.admin.admin.service.ShopAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ShopAdminServiceImpl extends ServiceImpl<ShopAdminDao, ShopAdmin> implements ShopAdminService {
    @Autowired
    private  ShopAdminDao shopAdminDao;
    @Override
    public Result doLogin(HttpServletRequest request, LoginReg loginReg) {
        //判断验证码是否正确
        String code = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
        if (!code.equalsIgnoreCase(loginReg.getCaptcha())){
           return  Result.fail("验证码错误！");
        }
        String md5Password = DigestUtils.md5DigestAsHex(loginReg.getAdminPass().getBytes());
        loginReg.setAdminPass(md5Password);
        //判断密码是否错误
        //List<ShopAdmin> list  = shopAdminDao.doLogin(loginReg);
        //第二种方法直接使用mybatis-plus封装的方法
        QueryWrapper<ShopAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name",loginReg.getAdminName());
        queryWrapper.eq("admin_pass",loginReg.getAdminPass());
//        queryWrapper.lambda().eq(ShopAdmin::getAdminName,loginReg.getAdminName());
        List<ShopAdmin> list = shopAdminDao.selectList(queryWrapper);
        if (list.size() != 1){
            return Result.fail("用户名或密码错误！");
        }
        request.getSession().setAttribute("adminId",list.get(0).getId());
        request.getSession().setAttribute("adminName",loginReg.getAdminName());
        return Result.ok("登录成功！");
    }

    @Override
    public Result doReg(HttpServletRequest request, LoginReg loginReg) {
        //判断验证码是否正确
        String code = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
        if (!code.equalsIgnoreCase(loginReg.getCaptcha())){
            return  Result.fail("验证码错误！");
        }
        String md5Password = DigestUtils.md5DigestAsHex(loginReg.getAdminPass().getBytes());
        loginReg.setAdminPass(md5Password);
        int count = shopAdminDao.selectByName(loginReg);
        //int count = shopAdminDao.selectCount(new QueryWrapper<ShopAdmin>().eq("admin_name",loginReg.getAdminName()));
        if (count > 0){
            return Result.fail("用户名重复！");
        }else{
            shopAdminDao.doReg(loginReg);
            //shopAdminDao.insert(loginReg);
            return Result.ok("注册成功！");
        }
    }

    @Override
    public ModelAndView editPass() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/editPass1");
        return mv;
    }

    @Override
    public Result doEditPass(HttpServletRequest request, EditPassReq editPass) {
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        String oldPwd = DigestUtils.md5DigestAsHex(editPass.getOldpwd().getBytes());
        String newPwd = DigestUtils.md5DigestAsHex(editPass.getNewpwd().getBytes());
        //查询当前密码是否正确
        QueryWrapper<ShopAdmin> queryWrapper = new QueryWrapper<ShopAdmin>();
        queryWrapper.lambda().eq(ShopAdmin::getAdminPass,oldPwd);
        queryWrapper.lambda().eq(ShopAdmin::getId,adminId);
        Integer count = shopAdminDao.selectCount(queryWrapper);
        if (count == 0){
            return Result.fail("原密码错误！");
        }
        //修改当前密码
        ShopAdmin shopAdmin = new ShopAdmin();
        shopAdmin.setId(adminId);
        shopAdmin.setAdminPass(newPwd);
        shopAdminDao.updateById(shopAdmin);
        return Result.ok("修改成功！");
    }
}
